package com.xbongbong.paas.script.controller;

import com.xbongbong.paas.annotation.AutoJob;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.script.pojo.dto.JobHealthDTO;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.sys.enums.JobStatusEnum;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/autoJob")
public class AutoJobController {
    private static String filePrefix = "autoJob";

    @Resource
    private PaasRedisHelper paasRedisHelper;

    @RequestMapping(value = "/jobHeath", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String jobHeath(@RequestBody @Valid JobHealthDTO jobHealthDTO) throws Exception {
        if (StringUtil.isEmpty(jobHealthDTO.getUrl())) {
            return JobStatusEnum.UNKNOW.getCode().toString();
        }
        String code = paasRedisHelper.getValue(filePrefix,jobHealthDTO.getUrl());
        if (Objects.isNull(code)) {
            return JobStatusEnum.UNKNOW.getCode().toString();
        }
        return code;
    }

    @AutoJob
    @RequestMapping(value = "/demo1", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void demo1() throws Exception {
        Thread.sleep(20);
    }

    @AutoJob
    @RequestMapping(value = "/demo2", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void demo2() throws Exception {
        Thread.sleep(10);
        String a = null;
        a.isEmpty();
    }


}
